﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace SampleTwitterStream
{
    public partial class Form1 : Form
    {

        //Working Twitter Streaming API URLS
        private const string _sampleTwitterStream = "https://stream.twitter.com/1.1/statuses/sample.json";
        private const string _getTwitterNumericId = "http://www.idfromuser.com/";
        private const string _testNumericId = "1264407572";
        private const string _filterTwitterStream = "https://stream.twitter.com/1.1/statuses/filter.json";
        private const string _defaultTwitterUserFeed = "https://stream.twitter.com/1.1/statuses/filter.json?follow=1264407572";

        private bool _pauseTwitter = false;
        int _counter = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void _twitterStreamButton_Click(object sender, EventArgs e)
        {
            string twitterApi = _twitterUrlTextBox.Text;
            if (String.IsNullOrEmpty(twitterApi))
            {
                MessageBox.Show("Please enter a valid twitter API URL.", "Twitter Tester");
                return;
            }
            string twitterUser = _twitterUsernameTextBox.Text;
            if (String.IsNullOrEmpty(twitterUser))
            {
                MessageBox.Show("Please enter a valid twitter username.", "Twitter Tester");
                return;
            }
            string twitterPassword = _twitterPasswordTextBox.Text;
            if (String.IsNullOrEmpty(twitterPassword))
            {
                MessageBox.Show("Please enter a valid twitter password.", "Twitter Tester");
                return;
            }


            WebRequest twitterRequest = WebRequest.Create(twitterApi);
            twitterRequest.Method = "POST";
            twitterRequest.ContentType = "application/json";
            twitterRequest.Credentials = new NetworkCredential(twitterUser, twitterPassword);
            Stream twitterRequestContent = twitterRequest.GetRequestStream();
            Encoding encode = Encoding.GetEncoding("utf-8");

            StreamWriter twitterWriter = new StreamWriter(twitterRequestContent);


            WebResponse twitterResponse = twitterRequest.GetResponse();


            StreamReader twitterStream = new StreamReader(twitterResponse.GetResponseStream(), encode);

            while (!twitterStream.EndOfStream && !_pauseTwitter && _counter < 3)
            {
                string twitterMessage = twitterStream.ReadLine();
                _twitterMessagesListBox.Items.Add(twitterMessage);
                _twitterMessagesListBox.Refresh();
                _counter++;
            }

        }

        private void _pauseTwitterButton_Click(object sender, EventArgs e)
        {
            _pauseTwitter = true;

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveTwitterMsgsToFileDialog.FileOk += new CancelEventHandler(saveTwitterMsgsToFileDialog_FileOk);
            saveTwitterMsgsToFileDialog.ShowDialog();
        }

        void saveTwitterMsgsToFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            //throw new NotImplementedException();
            StreamWriter twitterOutput = new StreamWriter(saveTwitterMsgsToFileDialog.FileName);
            for (int i = 0; i < _twitterMessagesListBox.Items.Count; i++)
            {
                string tweet = _twitterMessagesListBox.Items[i].ToString();
                twitterOutput.WriteLine(tweet);
            }
            twitterOutput.Flush();
            twitterOutput.Close();
        }

        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}
